package com.onlinemovies.service.implementation;

import java.io.Serializable;
import java.util.List;




import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.onlinemovies.entity.BookingDetails;
import com.onlinemovies.entity.MovieDetails;
import com.onlinemovies.entity.ShowDetails;
import com.onlinemovies.entity.User;
import com.onlinemovies.repository.extension.BookDetailsRepositary;
import com.onlinemovies.repository.extension.MovieRepository;
import com.onlinemovies.service.MovieService;

@Service("movieService")
@Transactional(readOnly=true)
public class MovieServiceImpl implements MovieService,Serializable {
	
	@Autowired
	private MovieRepository movieRepository;
	
	@Autowired
	private BookDetailsRepositary bookDetailsRepositary;

	@Override
	public List<MovieDetails> getAll() throws Exception {
		// TODO Auto-generated method stub
		List<MovieDetails> movdtls = movieRepository.getAll();
		return movdtls;
	}

	@Override
	public MovieDetails doInsert(MovieDetails movieDetails) throws Exception {
		// TODO Auto-generated method stub
		
		movieRepository.merge(movieDetails);
		return null;
	}

	@Override
	public Set<MovieDetails> findMovieForHall(Integer hallid) {
		System.out.println("hallid "+ hallid);
		Set<MovieDetails> movieDetails=movieRepository.getMovieListAgainstHallid(hallid);
		return movieDetails;
	}

	@Override
	public Set<ShowDetails> findfindShowForMovie(Integer movieid) {
		System.out.println("movieid "+ movieid);
		Set<ShowDetails> showDetails=movieRepository.getShowListAgainstMovieid(movieid);
		return showDetails;
	}

	@Override
	public String findPriceForMovie(Integer showid) {
		String price=movieRepository.findPriceForMovie(showid);
		return price;
	}

	@Override
	public String doInsertBookingDetails(BookingDetails bookingDetails) throws Exception{
		String message="";
		try {
			
			boolean flag=movieRepository.checkAvaliableSeat(bookingDetails);
			if(flag){
			bookDetailsRepositary.merge(bookingDetails);
				message="You have booked your ticket successfully. Enjoy the movie .";
			}else{
				message="Sorry..Seats are not avaliable.";
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw e;
		}
		return message;
	}

	@Override
	public List<BookingDetails> getBookingDetails(User user) {
		// TODO Auto-generated method stub
		return bookDetailsRepositary.getAll(user);
	}

}
